
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>文件操作 fs · JavaScript学习</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="Veng">
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-pageview-count/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-chapter-fold/chapter-fold.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-back-to-top-button/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-code/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="流Stream.html" />
    
    
    <link rel="prev" href="path.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../基础/基础.html">
            
                <a href="../基础/基础.html">
            
                    
                    基础
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../基础/变量.html">
            
                <a href="../基础/变量.html">
            
                    
                    数据类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../基础/let和const命令.html">
            
                <a href="../基础/let和const命令.html">
            
                    
                    let和const命令
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../基础/数值.html">
            
                <a href="../基础/数值.html">
            
                    
                    数值
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../基础/字符串.html">
            
                <a href="../基础/字符串.html">
            
                    
                    字符串
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="../基础/对象.html">
            
                <a href="../基础/对象.html">
            
                    
                    对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.6" data-path="../基础/数组.html">
            
                <a href="../基础/数组.html">
            
                    
                    数组
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.7" data-path="../基础/Symbol.html">
            
                <a href="../基础/Symbol.html">
            
                    
                    Symbol
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.8" data-path="../基础/函数.html">
            
                <a href="../基础/函数.html">
            
                    
                    函数
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.8.1" data-path="../基础/函数的扩展.html">
            
                <a href="../基础/函数的扩展.html">
            
                    
                    函数的扩展
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.8.2" data-path="../基础/变量的解构赋值.html">
            
                <a href="../基础/变量的解构赋值.html">
            
                    
                    变量作用域与解构赋值
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.8.3" data-path="../基础/函数3.html">
            
                <a href="../基础/函数3.html">
            
                    
                    方法函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.8.4" data-path="../基础/高阶函数.html">
            
                <a href="../基础/高阶函数.html">
            
                    
                    高阶函数
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.8.4.1" data-path="../基础/map-reduce.html">
            
                <a href="../基础/map-reduce.html">
            
                    
                    map/reduce
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.8.4.2" data-path="../基础/filter.html">
            
                <a href="../基础/filter.html">
            
                    
                    filter
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.8.4.3" data-path="../基础/sort.html">
            
                <a href="../基础/sort.html">
            
                    
                    sort
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.8.4.4" data-path="../基础/array其他方法.html">
            
                <a href="../基础/array其他方法.html">
            
                    
                    array其他方法
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2.8.5" data-path="../基础/闭包.html">
            
                <a href="../基础/闭包.html">
            
                    
                    闭包
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.8.6" data-path="../基础/箭头函数.html">
            
                <a href="../基础/箭头函数.html">
            
                    
                    箭头函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.8.7" data-path="../浏览器/Promise.html">
            
                <a href="../浏览器/Promise.html">
            
                    
                    Promise
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.8.8" data-path="../基础/generator.html">
            
                <a href="../基础/generator.html">
            
                    
                    generator
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.8.9" data-path="../基础/async.html">
            
                <a href="../基础/async.html">
            
                    
                    async
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2.9" data-path="../基础/标准对象.html">
            
                <a href="../基础/标准对象.html">
            
                    
                    标准对象
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.9.1" data-path="../基础/SetMap.html">
            
                <a href="../基础/SetMap.html">
            
                    
                    Set,Map
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.9.2" data-path="../基础/Date.html">
            
                <a href="../基础/Date.html">
            
                    
                    Date
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.9.3" data-path="../基础/Math.html">
            
                <a href="../基础/Math.html">
            
                    
                    Math
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.9.4" data-path="../基础/Proxy.html">
            
                <a href="../基础/Proxy.html">
            
                    
                    Proxy
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.9.5" data-path="../基础/RepExp.html">
            
                <a href="../基础/RepExp.html">
            
                    
                    正则表达式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.9.6" data-path="../基础/正则表达式.html">
            
                <a href="../基础/正则表达式.html">
            
                    
                    正则表达式别篇
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.9.7" data-path="../基础/json.html">
            
                <a href="../基础/json.html">
            
                    
                    JSON
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2.10" data-path="../基础/面向对象编程.html">
            
                <a href="../基础/面向对象编程.html">
            
                    
                    面向对象编程
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.10.1" data-path="../基础/对象的扩展.html">
            
                <a href="../基础/对象的扩展.html">
            
                    
                    对象的扩展
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.10.2" data-path="../基础/创建对象.html">
            
                <a href="../基础/创建对象.html">
            
                    
                    创建对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.10.3" data-path="../基础/原型继承.html">
            
                <a href="../基础/原型继承.html">
            
                    
                    原型继承
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.10.4" data-path="../基础/class继承.html">
            
                <a href="../基础/class继承.html">
            
                    
                    class继承
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.10.5" data-path="../基础/模块化Module.html">
            
                <a href="../基础/模块化Module.html">
            
                    
                    模块化Module
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2.11" data-path="../基础/异常处理.html">
            
                <a href="../基础/异常处理.html">
            
                    
                    异常处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.12" data-path="../基础/编程风格.html">
            
                <a href="../基础/编程风格.html">
            
                    
                    编程风格
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../js扩展/js扩展.html">
            
                <a href="../js扩展/js扩展.html">
            
                    
                    js 扩展
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../js扩展/字符串.html">
            
                <a href="../js扩展/字符串.html">
            
                    
                    字符串
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../浏览器/浏览器.html">
            
                <a href="../浏览器/浏览器.html">
            
                    
                    浏览器
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../浏览器/浏览器对象.html">
            
                <a href="../浏览器/浏览器对象.html">
            
                    
                    浏览器对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../浏览器/操作DOM.html">
            
                <a href="../浏览器/操作DOM.html">
            
                    
                    操作DOM
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.2.1" data-path="../浏览器/更新DOM.html">
            
                <a href="../浏览器/更新DOM.html">
            
                    
                    更新DOM
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2.2" data-path="../浏览器/插入DOM.html">
            
                <a href="../浏览器/插入DOM.html">
            
                    
                    插入DOM
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2.3" data-path="../浏览器/删除DOM.html">
            
                <a href="../浏览器/删除DOM.html">
            
                    
                    删除DOM
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../浏览器/DOM事件.html">
            
                <a href="../浏览器/DOM事件.html">
            
                    
                    DOM事件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="../浏览器/操作表单.html">
            
                <a href="../浏览器/操作表单.html">
            
                    
                    操作表单
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="../浏览器/操作文件.html">
            
                <a href="../浏览器/操作文件.html">
            
                    
                    操作文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="../浏览器/AJAX.html">
            
                <a href="../浏览器/AJAX.html">
            
                    
                    AJAX
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7" data-path="../浏览器/Canvas.html">
            
                <a href="../浏览器/Canvas.html">
            
                    
                    Canvas
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../Jquery/jquery.html">
            
                <a href="../Jquery/jquery.html">
            
                    
                    Jquery
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../Jquery/选择器.html">
            
                <a href="../Jquery/选择器.html">
            
                    
                    选择器
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1.1" data-path="../Jquery/层级选择器.html">
            
                <a href="../Jquery/层级选择器.html">
            
                    
                    层级选择器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.1.2" data-path="../Jquery/查找和过滤.html">
            
                <a href="../Jquery/查找和过滤.html">
            
                    
                    查找和过滤
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../Jquery/操作DOM.html">
            
                <a href="../Jquery/操作DOM.html">
            
                    
                    操作DOM
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.2.1" data-path="../Jquery/修改DOM结构.html">
            
                <a href="../Jquery/修改DOM结构.html">
            
                    
                    修改DOM结构
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../Jquery/事件.html">
            
                <a href="../Jquery/事件.html">
            
                    
                    事件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="../Jquery/动画.html">
            
                <a href="../Jquery/动画.html">
            
                    
                    动画
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="../Jquery/AJAX.html">
            
                <a href="../Jquery/AJAX.html">
            
                    
                    AJAX
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.6" data-path="../Jquery/扩展.html">
            
                <a href="../Jquery/扩展.html">
            
                    
                    扩展
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="nodejs.html">
            
                <a href="nodejs.html">
            
                    
                    Nodejs
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="模块.html">
            
                <a href="模块.html">
            
                    
                    模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="path.html">
            
                <a href="path.html">
            
                    
                    path
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.6.3" data-path="文件操作.html">
            
                <a href="文件操作.html">
            
                    
                    文件操作 fs
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.4" data-path="流Stream.html">
            
                <a href="流Stream.html">
            
                    
                    流Stream
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.5" data-path="zlib压缩解压缩.html">
            
                <a href="zlib压缩解压缩.html">
            
                    
                    zlib压缩解压缩
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.6" data-path="net网络.html">
            
                <a href="net网络.html">
            
                    
                    net网络
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.7" data-path="http.html">
            
                <a href="http.html">
            
                    
                    http
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.8" data-path="express框架.html">
            
                <a href="express框架.html">
            
                    
                    express框架
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.9" data-path="process进程.html">
            
                <a href="process进程.html">
            
                    
                    process 进程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.10" data-path="child_process.html">
            
                <a href="child_process.html">
            
                    
                    child_process
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.11" data-path="os.html">
            
                <a href="os.html">
            
                    
                    os-操作系统
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../react/react.html">
            
                <a href="../react/react.html">
            
                    
                    React
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >文件操作 fs</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x6587;&#x4EF6;&#x64CD;&#x4F5C;">&#x6587;&#x4EF6;&#x64CD;&#x4F5C;</h1>
<p>&#x6587;&#x4EF6;&#x64CD;&#x4F5C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;nodejs&#x7684;&#x5185;&#x7F6E; <code>fs</code>&#x6A21;&#x5757;&#x5B9E;&#x73B0;&#x3002;</p>
<p>&#x5E76;&#x4E14;&#x6240;&#x6709;&#x6587;&#x4EF6;&#x7CFB;&#x7EDF;&#x64CD;&#x4F5C;&#x90FD;&#x5177;&#x6709;&#x540C;&#x6B65;&#x548C;&#x5F02;&#x6B65;&#x7684;&#x5F62;&#x5F0F;&#x3002;</p>
<hr>
<h3 id="&#x8BFB;&#x53D6;&#x6587;&#x4EF6;">&#x8BFB;&#x53D6;&#x6587;&#x4EF6;</h3>
<p>&#x8BFB;&#x53D6;&#x6587;&#x4EF6;&#x5185;&#x5BB9;&#x7684;&#x51FD;&#x6570;&#x6709;&#x5F02;&#x6B65;&#x7684; fs.readFile() &#x548C;&#x540C;&#x6B65;&#x7684; fs.readFileSync()&#x3002;</p>
<p>&#x53D1;&#x73B0;&#x540C;&#x6B65;&#x8DDF;&#x5F02;&#x6B65;&#x7684;&#x65B9;&#x6CD5;&#x540D;&#x5C31;&#x662F;&#x540E;&#x9762;&#x76F8;&#x5DEE;&#x4E00;&#x4E2A;Sync&#x3002;&#x5176;&#x4ED6;&#x6587;&#x4EF6;&#x7684;&#x540C;&#x6B65;&#x5F02;&#x6B65;&#x64CD;&#x4F5C;&#x4EA6;&#x5982;&#x6B64;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">&quot;fs&quot;</span>);
<span class="hljs-comment">// &#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x4E3A;&#x6587;&#x4EF6;&#x8DEF;&#x5F84;&#xFF0C;&#x7B2C;&#x4E8C;&#x4E2A;&#x53C2;&#x6570;&#x4E3A;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x3002;</span>
<span class="hljs-comment">// error&#x662F;&#x8BFB;&#x53D6;&#x51FA;&#x9519;&#x7684;&#x5F02;&#x5E38;&#xFF0C;&#x5982;&#x679C;&#x6CA1;&#x6709;&#x51FA;&#x9519;&#xFF0C;&#x5219;error&#x4E3A;undifine&#x3002;</span>
<span class="hljs-comment">// data &#x4E3A;&#x6587;&#x4EF6;&#x7684;&#x6570;&#x636E;&#x3002;</span>
fs.readFile(<span class="hljs-string">&quot;test&quot;</span>, (error, data) =&gt; {
    <span class="hljs-keyword">if</span> (error) {
        <span class="hljs-built_in">console</span>.log(error);
    } <span class="hljs-keyword">else</span> {
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`&#x5F02;&#x6B65;&#x8BFB;&#x53D6;:<span class="hljs-subst">${data.toString()}</span>`</span>);
    }
});
<span class="hljs-comment">// &#x540C;&#x6B65;&#x8BFB;&#x53D6;&#xFF0C;&#x8981;try&#xFF0C;catch&#x4E0B;&#xFF0C;&#x6355;&#x83B7;&#x5F02;&#x5E38;&#x3002;</span>
<span class="hljs-keyword">try</span> {
    <span class="hljs-keyword">const</span> data = fs.readFileSync(<span class="hljs-string">&quot;test&quot;</span>);
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`&#x540C;&#x6B65;&#x8BFB;&#x53D6;:<span class="hljs-subst">${data.toString()}</span>`</span>);
} <span class="hljs-keyword">catch</span> (e) {
    <span class="hljs-built_in">console</span>.log(e);
}

<span class="hljs-comment">/*
&#x540C;&#x6B65;&#x8BFB;&#x53D6;:hahaha
&#x5F02;&#x6B65;&#x8BFB;&#x53D6;:hahaha
*/</span>
</code></pre>
<p>&#x4EE5;&#x4E0A;&#x53EF;&#x4EE5;&#x770B;&#x51FA;&#xFF0C;&#x867D;&#x7136;&#x5F02;&#x6B65;&#x8BFB;&#x53D6;&#x4EE3;&#x7801;&#x5199;&#x5728;&#x524D;&#x9762;&#xFF0C;&#x4F46;&#x662F;&#x5B9E;&#x9645;&#x4E0A;&#xFF0C;&#x540C;&#x6B65;&#x8BFB;&#x53D6;&#x5148;&#x8FDB;&#x884C;&#xFF0C;&#x56E0;&#x4E3A;&#x5F02;&#x6B65;&#x8BFB;&#x53D6;&#x5E76;&#x6CA1;&#x963B;&#x585E;&#x7EBF;&#x7A0B;&#x3002;</p>
<hr>
<h3 id="&#x6587;&#x4EF6;&#x5199;&#x5165;">&#x6587;&#x4EF6;&#x5199;&#x5165;</h3>
<p>&#x5F02;&#x6B65;&#x6A21;&#x5F0F;&#x4E0B;&#x5199;&#x5165;&#x8BED;&#x6CD5;&#x4E3A;&#xFF1A;</p>
<pre><code class="lang-js">fs.writeFile(file, data[, options], callback)
<span class="hljs-comment">// &#x540C;&#x6B65;&#x5199;&#x5165;</span>
fs.writeFileSync(file, data[, options])
</code></pre>
<ul>
<li><strong>file</strong> - &#x6587;&#x4EF6;&#x540D;&#x6216;&#x6587;&#x4EF6;&#x63CF;&#x8FF0;&#x7B26;&#x3002;</li>
<li><strong>data</strong> - &#x8981;&#x5199;&#x5165;&#x6587;&#x4EF6;&#x7684;&#x6570;&#x636E;&#xFF0C;&#x53EF;&#x4EE5;&#x662F; String(&#x5B57;&#x7B26;&#x4E32;) &#x6216; Buffer(&#x7F13;&#x51B2;) &#x5BF9;&#x8C61;&#x3002;</li>
<li><strong>options</strong> - &#x8BE5;&#x53C2;&#x6570;&#x662F;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#xFF0C;&#x5305;&#x542B; {encoding, mode, flag}&#x3002;&#x9ED8;&#x8BA4;&#x7F16;&#x7801;&#x4E3A; utf8, &#x6A21;&#x5F0F;&#x4E3A; 0666 &#xFF0C; flag &#x4E3A; &apos;w&apos;</li>
<li><strong>callback</strong> - &#x56DE;&#x8C03;&#x51FD;&#x6570;&#xFF0C;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x53EA;&#x5305;&#x542B;&#x9519;&#x8BEF;&#x4FE1;&#x606F;&#x53C2;&#x6570;(err)&#xFF0C;&#x5728;&#x5199;&#x5165;&#x5931;&#x8D25;&#x65F6;&#x8FD4;&#x56DE;&#x3002;</li>
</ul>
<p>&#x4F8B;&#x5982;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;fs&apos;</span>);

fs.writeFile(<span class="hljs-string">&quot;write-file&quot;</span>,<span class="hljs-string">&quot;123456&quot;</span>, error=&gt;{
    <span class="hljs-keyword">if</span> (error){
        <span class="hljs-built_in">console</span>.log(error);
    }<span class="hljs-keyword">else</span>{
        <span class="hljs-keyword">const</span> data = fs.readFileSync(<span class="hljs-string">&apos;write-file&apos;</span>);
        <span class="hljs-built_in">console</span>.log(data.toString());
    }
});
<span class="hljs-comment">/**
123456
*/</span>
</code></pre>
<p>&#x5148;&#x5199;&#x5165;&#x6587;&#x4EF6;&#xFF0C;&#x7136;&#x540E;&#x8BFB;&#x53D6;&#x51FA;&#x6765;.</p>
<h4 id="&#x8FFD;&#x52A0;&#x5199;&#x5165;">&#x8FFD;&#x52A0;&#x5199;&#x5165;</h4>
<p>&#x5C06;&#x6570;&#x636E;&#x8FFD;&#x52A0;&#x5230;&#x6587;&#x4EF6;,&#x5982;&#x679C;&#x6587;&#x4EF6;&#x5C1A;&#x4E0D;&#x5B58;&#x5728;&#x5219;&#x521B;&#x5EFA;&#x8BE5;&#x6587;&#x4EF6;</p>
<pre><code class="lang-js"><span class="hljs-comment">// &#x5F02;&#x6B65;&#x8FFD;&#x52A0;</span>
fs.appendFile(path, data[, options], callback)
<span class="hljs-comment">// &#x540C;&#x6B65;&#x5199;&#x5165;</span>
fs.appendFileSync(path, data[, options]);
</code></pre>
<p>&#x53C2;&#x6570;&#x8BF4;&#x660E;:</p>
<ul>
<li>path - &#x6587;&#x4EF6;&#x8DEF;&#x5F84;</li>
<li>data - &#x8FFD;&#x52A0;&#x7684;&#x6570;&#x636E;</li>
<li>options - &#x914D;&#x7F6E;&#x9879; &#x53EF;&#x4EE5;&#x662F;string,&#x4E5F;&#x53EF;&#x4EE5;&#x662F;object<ul>
<li>string - &#x7F16;&#x7801;&#x65B9;&#x5F0F; &#x9ED8;&#x8BA4;&#x503C;: <code>utf-8</code>.</li>
<li>object <ul>
<li>encoding - &#x7F16;&#x7801;&#x65B9;&#x5F0F; &#x9ED8;&#x8BA4;&#x503C;: <code>utf-8</code>.</li>
<li>mode - &#x9ED8;&#x8BA4;&#x503C;:<code>0o666</code>.(&#x4E00;&#x822C;&#x4E0D;&#x7528;&#x914D;&#x7F6E;).</li>
<li>flag - &#x6587;&#x4EF6;&#x7CFB;&#x7EDF;&#x6807;&#x5FD7; &#x9ED8;&#x8BA4;&#x503C;: <code>a</code>,&#x8868;&#x793A;&#x8FFD;&#x52A0;.</li>
</ul>
</li>
</ul>
</li>
<li>callback - function(error) &#x56DE;&#x8C03;&#x51FD;&#x6570;</li>
</ul>
<hr>
<h3 id="&#x83B7;&#x53D6;&#x6587;&#x4EF6;&#x4FE1;&#x606F;">&#x83B7;&#x53D6;&#x6587;&#x4EF6;&#x4FE1;&#x606F;</h3>
<p>&#x5F02;&#x6B65;&#x83B7;&#x53D6;&#x6587;&#x4EF6;&#x4FE1;&#x606F;&#x8BED;&#x6CD5;&#xFF1A;</p>
<pre><code class="lang-js">fs.stat(path, callback)
</code></pre>
<p>&#x4F8B;&#x5982;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">&quot;fs&quot;</span>);

fs.stat(<span class="hljs-string">`test`</span>,(error,stats)=&gt;{
    <span class="hljs-keyword">if</span> (error) {
        <span class="hljs-built_in">console</span>.log(error);
    }<span class="hljs-keyword">else</span>{
        <span class="hljs-built_in">console</span>.log(stats);
    }
});

<span class="hljs-comment">/*
Stats {
  dev: 2066,
  mode: 33204,
  nlink: 1,
  uid: 1000,
  gid: 1000,
  rdev: 0,
  blksize: 4096,
  ino: 13910026,
  size: 6,
  blocks: 8,
  atimeMs: 1569741090778.4666,
  mtimeMs: 1569741090466.4666,
  ctimeMs: 1569741090466.4666,
  birthtimeMs: 1569741090466.4666,
  atime: 2019-09-29T07:11:30.778Z,
  mtime: 2019-09-29T07:11:30.466Z,
  ctime: 2019-09-29T07:11:30.466Z,
  birthtime: 2019-09-29T07:11:30.466Z }
*/</span>
</code></pre>
<p>stats&#x7C7B;&#x5305;&#x542B;&#x4E86;&#x6587;&#x4EF6;&#x7684;&#x5404;&#x79CD;&#x4FE1;&#x606F;&#xFF0C;&#x8FD8;&#x63D0;&#x4F9B;&#x4E86;&#x6709;&#x7528;&#x7684;&#x65B9;&#x6CD5;&#xFF1A;&#x5982;</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">&quot;fs&quot;</span>);

fs.stat(<span class="hljs-string">`test`</span>,(error,stats)=&gt;{
    <span class="hljs-keyword">if</span> (error) {
        <span class="hljs-built_in">console</span>.log(error);
    }<span class="hljs-keyword">else</span>{
        <span class="hljs-built_in">console</span>.log(stats.isFile()); <span class="hljs-comment">// true</span>
        <span class="hljs-built_in">console</span>.log(stats.isDirectory()); <span class="hljs-comment">// false</span>
    }
});
</code></pre>
<hr>
<h3 id="&#x6587;&#x4EF6;&#x7684;&#x5220;&#x9664;">&#x6587;&#x4EF6;&#x7684;&#x5220;&#x9664;</h3>
<p>unlink&#xFF1A;&#x5F02;&#x6B65;&#x5220;&#x9664;&#x6587;&#x4EF6;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;fs&apos;</span>);
fs.unlink(<span class="hljs-string">&apos;./test.txt&apos;</span>,err=&gt;{
    <span class="hljs-keyword">if</span> (err) {
        <span class="hljs-built_in">console</span>.log(err.message);
    }<span class="hljs-keyword">else</span>{
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`&#x5220;&#x9664;&#x6210;&#x529F;!`</span>);
    }
});
<span class="hljs-comment">// =&gt; &#x5220;&#x9664;&#x6210;&#x529F;!</span>
</code></pre>
<p>&#x540C;&#x6B65;&#x5220;&#x9664;&#x6587;&#x4EF6;</p>
<p>&#x4F7F;&#x7528;&#x540C;&#x6B65;&#x7684;&#x64CD;&#x4F5C;&#x53D1;&#x751F;&#x7684;&#x5F02;&#x5E38;&#x4F1A;&#x7ACB;&#x5373;&#x629B;&#x51FA;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>try&#x2026;catch</code> &#x5904;&#x7406;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x5141;&#x8BB8;&#x5192;&#x6CE1;&#x3002;</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;fs&apos;</span>);

<span class="hljs-keyword">try</span> {
  fs.unlinkSync(<span class="hljs-string">&apos;/tmp/hello&apos;</span>);
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x5DF2;&#x6210;&#x529F;&#x5220;&#x9664; /tmp/hello&apos;</span>);
} <span class="hljs-keyword">catch</span> (err) {
  <span class="hljs-comment">// &#x5904;&#x7406;&#x9519;&#x8BEF;</span>
}
</code></pre>
<p><font color="red">&#x6CE8;&#x610F;</font>&#xFF1A;&#x4F7F;&#x7528;&#x5F02;&#x6B65;&#x7684;&#x65B9;&#x6CD5;&#x65F6;&#x65E0;&#x6CD5;&#x4FDD;&#x8BC1;&#x987A;&#x5E8F;&#x3002; &#x56E0;&#x6B64;&#xFF0C;&#x5BF9;&#x540C;&#x4E00;&#x4E2A;&#x6587;&#x4EF6;&#x7684;&#x64CD;&#x4F5C;&#x5BB9;&#x6613;&#x51FA;&#x9519;&#x3002;</p>
<hr>
<h3 id="&#x521B;&#x5EFA;&#x76EE;&#x5F55;">&#x521B;&#x5EFA;&#x76EE;&#x5F55;</h3>
<p>&#x8BED;&#x6CD5;&#xFF1A;</p>
<pre><code class="lang-js">fs.mkdir(path[,options],callback)
</code></pre>
<h4 id="&#x53C2;&#x6570;">&#x53C2;&#x6570;</h4>
<p>&#x53C2;&#x6570;&#x4F7F;&#x7528;&#x8BF4;&#x660E;&#x5982;&#x4E0B;&#xFF1A;</p>
<ul>
<li><strong>path</strong> - &#x6587;&#x4EF6;&#x8DEF;&#x5F84;&#x3002;</li>
<li>options &#x53C2;&#x6570;&#x53EF;&#x4EE5;&#x662F;&#xFF1A;<ul>
<li><strong>recursive</strong> - &#x662F;&#x5426;&#x4EE5;&#x9012;&#x5F52;&#x7684;&#x65B9;&#x5F0F;&#x521B;&#x5EFA;&#x76EE;&#x5F55;&#xFF0C;&#x9ED8;&#x8BA4;&#x4E3A; false&#x3002;</li>
<li><strong>mode</strong> - &#x8BBE;&#x7F6E;&#x76EE;&#x5F55;&#x6743;&#x9650;&#xFF0C;&#x9ED8;&#x8BA4;&#x4E3A; 0777&#x3002;</li>
</ul>
</li>
<li><strong>callback</strong> - &#x56DE;&#x8C03;&#x51FD;&#x6570;&#xFF0C;&#x6CA1;&#x6709;&#x53C2;&#x6570;&#x3002;</li>
</ul>
<p>&#x4F8B;&#x5982;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">&quot;fs&quot;</span>);

fs.mkdir(<span class="hljs-string">&apos;dir&apos;</span>,error=&gt;{
    <span class="hljs-keyword">if</span> (error) {
        <span class="hljs-built_in">console</span>.log(error);
    }<span class="hljs-keyword">else</span>{
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`&#x521B;&#x5EFA;&#x76EE;&#x5F55;&#x6210;&#x529F;!`</span>);
    }
});
</code></pre>
<p>&#x5982;&#x679C;&#x76EE;&#x5F55;&#x5B58;&#x5728;&#x5219;&#x4F1A;&#x51FA;&#x9519;&#xFF0C;&#x5982;&#x679C;&#x60F3;&#x8981;&#x4E0D;&#x7BA1;&#x76EE;&#x5F55;&#x662F;&#x5426;&#x5B58;&#x5728;&#x53EF;&#x4EE5;&#x6DFB;&#x52A0; recursive: true.</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">&quot;fs&quot;</span>);

fs.mkdir(<span class="hljs-string">&apos;dir&apos;</span>,{recursive: <span class="hljs-literal">true</span>},error=&gt;{
    <span class="hljs-keyword">if</span> (error) {
        <span class="hljs-built_in">console</span>.log(error);
    }<span class="hljs-keyword">else</span>{
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`&#x521B;&#x5EFA;&#x76EE;&#x5F55;&#x6210;&#x529F;!`</span>);
    }
});
</code></pre>
<hr>
<h3 id="&#x8BFB;&#x53D6;&#x76EE;&#x5F55;">&#x8BFB;&#x53D6;&#x76EE;&#x5F55;</h3>
<p>&#x8BED;&#x6CD5;&#xFF1A;</p>
<pre><code class="lang-js">fs.readdir(path, callback)
</code></pre>
<h4 id="&#x53C2;&#x6570;">&#x53C2;&#x6570;</h4>
<p>&#x53C2;&#x6570;&#x4F7F;&#x7528;&#x8BF4;&#x660E;&#x5982;&#x4E0B;&#xFF1A;</p>
<ul>
<li><strong>path</strong> - &#x6587;&#x4EF6;&#x8DEF;&#x5F84;&#x3002;</li>
<li><strong>callback</strong> - &#x56DE;&#x8C03;&#x51FD;&#x6570;&#xFF0C;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x5E26;&#x6709;&#x4E24;&#x4E2A;&#x53C2;&#x6570;err, files&#xFF0C;err &#x4E3A;&#x9519;&#x8BEF;&#x4FE1;&#x606F;&#xFF0C;files &#x4E3A; &#x76EE;&#x5F55;&#x4E0B;&#x7684;&#x6587;&#x4EF6;&#x6570;&#x7EC4;&#x5217;&#x8868;&#x3002;</li>
</ul>
<p>&#x5982;,&#x8BFB;&#x53D6;&#x6839;&#x76EE;&#x5F55;(Linux&#x7CFB;&#x7EDF;)&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;fs&apos;</span>);

fs.readdir(<span class="hljs-string">&apos;/&apos;</span>,(error,files)=&gt;{
    <span class="hljs-keyword">if</span> (error) {
        <span class="hljs-built_in">console</span>.log(error);
    }<span class="hljs-keyword">else</span>{
        files.forEach(file=&gt;{
            <span class="hljs-built_in">console</span>.log(file);
        })
    }
});
<span class="hljs-comment">/*
bin
boot
cdrom
dev
etc
home
...
*/</span>
</code></pre>
<hr>
<h3 id="&#x5220;&#x9664;&#x76EE;&#x5F55;">&#x5220;&#x9664;&#x76EE;&#x5F55;</h3>
<pre><code class="lang-js">fs.rmdir(path, callback)
</code></pre>
<ul>
<li><strong>path</strong> - &#x6587;&#x4EF6;&#x8DEF;&#x5F84;&#x3002;</li>
<li><strong>callback</strong> - &#x56DE;&#x8C03;&#x51FD;&#x6570;&#xFF0C;error&#x53C2;&#x6570;.</li>
</ul>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;fs&apos;</span>);

fs.rmdir(<span class="hljs-string">&quot;dir&quot;</span>,error=&gt;{
    <span class="hljs-keyword">if</span> (error) {
        <span class="hljs-built_in">console</span>.log(error);
    }
});
</code></pre>
<p>&#x66F4;&#x591A;&#x64CD;&#x4F5C;&#x53C2;&#x8003;<a href="https://www.runoob.com/nodejs/nodejs-fs.html" target="_blank">&#x83DC;&#x9E1F;&#x6559;&#x7A0B;</a>.</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="path.html" class="navigation navigation-prev " aria-label="Previous page: path">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="流Stream.html" class="navigation navigation-next " aria-label="Next page: 流Stream">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"文件操作 fs","level":"1.6.3","depth":2,"next":{"title":"流Stream","level":"1.6.4","depth":2,"path":"nodejs/流Stream.md","ref":"nodejs/流Stream.md","articles":[]},"previous":{"title":"path","level":"1.6.2","depth":2,"path":"nodejs/path.md","ref":"nodejs/path.md","articles":[]},"dir":"ltr"},"config":{"plugins":["lunr","pageview-count","chapter-fold","back-to-top-button","code","hide-element","-sharing","sharing-plus","custom-favicon"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"chapter-fold":{},"search":{},"sharing-plus":{"qq":false,"all":["facebook","google","twitter","instapaper","linkedin","pocket","stumbleupon"],"douban":false,"facebook":true,"weibo":false,"instapaper":false,"whatsapp":false,"hatenaBookmark":false,"twitter":true,"messenger":false,"line":false,"vk":false,"pocket":true,"google":false,"viber":false,"stumbleupon":false,"qzone":false,"linkedin":false},"lunr":{"ignoreSpecialCharacters":false,"maxIndexSize":1000000},"code":{"copyButtons":true},"hide-element":{"elements":[".gitbook-link"]},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"favicon":"js.ico","back-to-top-button":{},"pageview-count":{},"custom-favicon":{},"sharing":{"qq":true,"all":["douban","facebook","google","instapaper","linkedin","twitter","weibo","messenger","qq","qzone","viber","whatsapp"],"douban":true,"facebook":true,"weibo":true,"twitter":true,"pocket":true,"google":true,"qzone":true},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"Veng","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"JavaScript学习","language":"zh-hans","gitbook":"*","description":"javaScript的学习笔记"},"file":{"path":"nodejs/文件操作.md","mtime":"2020-12-10T02:10:04.990Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-12-10T03:00:51.663Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-pageview-count/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-chapter-fold/chapter-fold.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-back-to-top-button/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-code/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-hide-element/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing-plus/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

